﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using MyWeb.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Common;
using MyWeb.Business;
namespace MyWeb.Admins
{
    public partial class tblUser : System.Web.UI.Page
    {
        static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["folder"] = "tblUser";
                BindGrid();
            }
        }
        private void BindGrid()
        {
            grdtblUser.DataSource = tblUserService.tblUser_GetByAll() ;
            grdtblUser.DataBind();
        }
        protected void btnThem_Click(object sender, EventArgs e)
        {
            pnShow.Visible = false;
            pnUpdate.Visible = true;
            ControlClass.ResetControlValues(this);
            Session["upload"] = null;
            Insert=true;
        }
        protected void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridItem item = default(DataGridItem);
                for (int i = 0; i < grdtblUser.Items.Count; i++)
                {
                    item = grdtblUser.Items[i];
                    if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
                    {
                        if (((CheckBox)item.FindControl("ChkSelect")).Checked)
                        {
                            string strId = item.Cells[1].Text;
                            try
                            {
                                tblUserService.tblUser_Delete(strId);
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                grdtblUser.CurrentPageIndex = 0;
                BindGrid();
            }
            catch
            {
            }
        }
        protected void grdtblUser_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            Data.tblUserInfo strCA=new tblUserInfo();
            strCA._uid = e.CommandArgument.ToString();
            if  (e.CommandName=="Edit")
            {
                DataTable dt = tblUserService.tblUser_GetById(strCA);
                txt_uid.Text=dt.Rows[0]["_uid"].ToString();
                txt_uname.Text=dt.Rows[0]["_uname"].ToString();
                txt_uaccount.Text=dt.Rows[0]["_uaccount"].ToString();
                txt_upassword.Text=dt.Rows[0]["_upassword"].ToString();
                txt_ustatus.Text=dt.Rows[0]["_ustatus"].ToString();
                txt_ucreatedate.Text=dt.Rows[0]["_ucreatedate"].ToString();
                pnUpdate.Visible = true;pnShow.Visible = false;
            }
            else
            {
                tblUserService.tblUser_Delete(strCA._uid);
                BindGrid();
            }
        }
        protected void lblBack_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
            pnShow.Visible = true;
            Insert=false;
        }
        protected void lblUpdate_Click(object sender, EventArgs e)
        {
            string image = Session["upload"] == null ? "" : Session["upload"].ToString();
    #region[TestInput]
            if (txt_uid.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("_uid không thể trống !");
                txt_uid.Focus();
                return;
            }
            if (txt_uname.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("_uname không thể trống !");
                txt_uname.Focus();
                return;
            }
            if (txt_uaccount.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("_uaccount không thể trống !");
                txt_uaccount.Focus();
                return;
            }
            if (txt_upassword.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("_upassword không thể trống !");
                txt_upassword.Focus();
                return;
            }
            if (txt_ustatus.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("_ustatus không thể trống !");
                txt_ustatus.Focus();
                return;
            }
            if (txt_ucreatedate.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("_ucreatedate không thể trống !");
                txt_ucreatedate.Focus();
                return;
            }
#endregion
            Data.tblUserInfo obj=new Data.tblUserInfo();
            obj._uid = txt_uid.Text;
            obj._uname = txt_uname.Text;
            obj._uaccount = txt_uaccount.Text;
            obj._upassword = txt_upassword.Text;
            obj._ustatus = txt_ustatus.Text;
            obj._ucreatedate = txt_ucreatedate.Text;
            if (Insert == true)
            {
                tblUserService.tblUser_Insert(obj);
            }
            else
            {
                tblUserService.tblUser_Update(obj);
            }
            BindGrid();
            pnShow.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
        }
        protected void grdtblUser_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdtblUser.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }
        protected void grdtblUser_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll");
                    if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                    }
                }
                else
                {
                    string tableRowId = grdtblUser.ClientID + "_row" + e.Item.ItemIndex.ToString();
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                    if ((checkBox != null))
                    {
                        e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                        e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex + ")");
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex + ")");
                    }
                }
            }
        }
    }
}